<?php
header('Content-Type: application/json');

$user = isset($_COOKIE['enorg']) ? $_COOKIE['enorg'] : 'bozo'; 
$promptsDir = __DIR__ . '/../'.$user.'-output/GEO';
$prompts = [];

// 检查目录是否存在
if (is_dir($promptsDir)) {
    $files = scandir($promptsDir);
    
    if ($files) {
        // Sort files to ensure a consistent order
        sort($files);
        foreach ($files as $file) {
            // 跳过当前目录和父目录
            if ($file === '.' || $file === '..') {
                continue;
            }
            
            // 支持读取.txt和.md文件
            if (in_array(pathinfo($file, PATHINFO_EXTENSION), ['txt', 'md'])) {
                $content = file_get_contents($promptsDir . '/' . $file);
                // Use filename without extension as title
                $title = pathinfo($file, PATHINFO_FILENAME);
                $prompts[] = ['title' => $title, 'content' => $content];
            }
        }
    }
    
    echo json_encode(['success' => true, 'prompts' => $prompts]);
} else {
    echo json_encode(['success' => false, 'message' => '目录不存在: ' . $promptsDir]);
}
?>